package com.hm.service.config.microservice;


import com.hm.service.impl.UserServiceImpl;
import com.hm.service.api.user.UserService;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.config.*;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
public class ServerConfig {

    private static final Logger logger = LoggerFactory.getLogger(ServerConfig.class);

    @Bean(name = {"ServerConfig"})
    public DubboBootstrap init() {
        logger.warn("=======>ServerConfig初始化了<=======");

        // 启动 Dubbo
        DubboBootstrap bootstrap = DubboBootstrap.getInstance().application(getApplicationConfig());
        bootstrap.registry(getRegistryConfig());
        bootstrap.protocol(getProtocolConfig());
        bootstrap.service(getUserService());
        bootstrap.start();
        bootstrap.await();

        return bootstrap;
    }


    private static ServiceConfig<UserService> getUserService() {
        ServiceConfig<UserService> service = new ServiceConfig<>();
        service.setInterface(UserService.class);
        service.setRef(new UserServiceImpl());
        service.setLoadbalance("roundrobin");
        service.setVersion("1.0.1");
        return service;
    }

    /**
     * 获取注册中心配置
     *
     * @return RegistryConfig
     */
    private static RegistryConfig getRegistryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("nacos://172.26.2.86:8848");
        registryConfig.setGroup("YDCG_SERVICE_GROUP");
        registryConfig.setUsername("nacos");
        registryConfig.setPassword("nacos");
        registryConfig.setRegisterMode("instance/interface/all");//改变注册行为,只需要应用级服务
        Map<String, String> parameters = new HashMap<>();
        parameters.put("namespace", "5cbb70a5-xxx-xxx-xxx-d43479ae0932");
        registryConfig.setParameters(parameters);
        registryConfig.setTimeout(30 * 1000);//连接超时时间，默认 30s
        registryConfig.setSession(60 * 1000);//会话超时时间，默认 60s
        return registryConfig;
    }

    /**
     * 协议配置
     *
     * @return ProtocolConfig
     */
    private static ProtocolConfig getProtocolConfig() {
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setName("dubbo");
        protocolConfig.setPort(-1);//随机端口
        int cores = Runtime.getRuntime().availableProcessors();
        protocolConfig.setThreads(cores * 100);//设置线程数量
        return protocolConfig;
    }

    /**
     * 应用配置
     *
     * @return ApplicationConfig
     */
    private static ApplicationConfig getApplicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("user-service-provider");
        applicationConfig.setQosEnable(false);//关闭QOS
        //applicationConfig.setProtocol("2001");
        return applicationConfig;
    }

}
